package com.isunland.managesystem.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.TravelListContent;
import java.util.List;

public class TravelListtAdapter extends ArrayAdapter<TravelListContent> {
  private static class ViewHolder {
    public final LinearLayout rootView;
    public final TextView tvCertificateName;
    public final TextView tvExpenseDesc;
    public final TextView tvLoanMoney;
    public final TextView tvPlanStatus;

    private ViewHolder(LinearLayout rootView, TextView tvCertificateName, TextView tvExpenseDesc,
        TextView tvLoanMoney, TextView tvPlanStatus) {
      this.rootView = rootView;
      this.tvCertificateName = tvCertificateName;
      this.tvExpenseDesc = tvExpenseDesc;
      this.tvLoanMoney = tvLoanMoney;
      this.tvPlanStatus = tvPlanStatus;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView tvCertificateName = (TextView) rootView.findViewById(R.id.tv_certificate_name);
      TextView tvExpenseDesc = (TextView) rootView.findViewById(R.id.tv_expense_desc);
      TextView tvLoanMoney = (TextView) rootView.findViewById(R.id.tv_loanMoney);
      TextView tvPlanStatus = (TextView) rootView.findViewById(R.id.tv_planStatus);
      return new ViewHolder(rootView, tvCertificateName, tvExpenseDesc, tvLoanMoney, tvPlanStatus);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_employee_loan, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    TravelListContent item = getItem(position);
    //地点
    StringBuilder place = new StringBuilder();
    if ("".equals(item.getLEAVE_PLACE()) && "".equals(item.getARRIVE_PLACE())) {
      place.append("");
    } else {
      place.append(item.getLEAVE_PLACE()).append("-").append(item.getARRIVE_PLACE());
    }

    //时间
    StringBuilder time = new StringBuilder();
    if ("".equals(item.getLEAVE_TIME()) && "".equals(item.getARRIVE_TIME())) {
      time.append("");
    } else {
      time.append(item.getLEAVE_TIME()).append("/").append(item.getARRIVE_TIME());
    }

    if (("日常费用报销").equals(item.getSUBDOCTYPE())) {
      vh.tvCertificateName.setText(item.getSUBDOCTYPE());//出差事由
      vh.tvExpenseDesc.setText(item.getFEE_NODE());//费用说明
      vh.tvLoanMoney.setText(item.getFEE_AMOUNT());//金额
    } else if (("差旅费用报销").equals(item.getSUBDOCTYPE())) {
      vh.tvCertificateName.setText(item.getSUBDOCTYPE());//出差事由
      vh.tvExpenseDesc.setText(place.toString());//地点
      vh.tvLoanMoney.setText(time.toString());//时间
    }

    vh.tvPlanStatus.setVisibility(View.GONE);

    return vh.rootView;
  }

  private LayoutInflater mInflater;

  // Constructors
  public TravelListtAdapter(Context context, List<TravelListContent> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  public TravelListtAdapter(Context context, TravelListContent[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }
}